"""
css选择器
xpath选择器
"""
from lxml import etree

html_str = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>11111111111111111111111</title>
</head>
<body>
<ul>
    <li id="li1">li1</li>
    <li id="li2">li2</li>
    <li id="li3">li3</li>
</ul>
<ol id="ol1">
    <li id="li11"><a href="https://www.baidu.com">百度</a></li>
    <li id="li22"><a href="https://www.jd.com">京东</a></li>
    <li id="li33"><a href="https://www.taobao.com">淘宝</a></li>
</ol>
<ol id="ol2">
    <li id="li111"><a href="https://www.baidu.com">百度</a></li>
    <li id="li222"><a href="https://www.jd.com">京东</a></li>
    <li id="li333"><a href="https://www.taobao.com">淘宝</a></li>
</ol>

</body>
</html>
"""
# cssselect结果为列表 每项类型都为<class 'lxml.etree._Element'> 可进行更深查找
tree = etree.HTML(html_str)
# title = tree.cssselect('title')[0]
# print(type(title))
# lis = tree.cssselect('li')[0]
# print(type(lis))
# lis = tree.cssselect('li')
# # for li in lis:
# #     print(li.text,li.attrib,li.tag)
#
# ol_lis = tree.cssselect('ol li')
# for li in ol_lis:
#     print(li.text, li.attrib, li.tag)
#     a = li.cssselect('a')
#     print(a[0].text, a[0].attrib, a[0].tag)

# html = tree.xpath('/html')[0]
# print(html.tag)
# title = html.xpath('./head/title')[0]
# print(title.tag)
# ul = tree.xpath('//ul')[0]
# print(ul.tag)
# ul_lis =ul.xpath('./li')
# for li in ul_lis:
#     print(li.text,li.attrib)
#
# ol1_lis = ul.xpath('../ol[@id="ol1"]/li')
# for li in ol1_lis:
#     print(li.text,li.attrib)
#
# ol2_lis = ul.xpath('//ol[@id="ol2"]/li')
# for li in ol2_lis:
#     print(li.text,li.attrib)

# lis = tree.xpath('//li[1]')
# for li in lis:
#     print(li.text,li.attrib)

# lis = tree.xpath('//li[last()]')
# for li in lis:
#     print(li.text,li.attrib)
#
# lis = tree.xpath('//li[last()-1]')
# for li in lis:
#     print(li.text,li.attrib)


# lis = tree.xpath('//li[position()>1]')
# for li in lis:
#     print(li.text,li.attrib)

# lis = tree.xpath('//li[position()>2 or position()<2]')
# for li in lis:
#     print(li.text,li.attrib)

# lis = tree.xpath('//a[contains(@href,"j")]')
# for li in lis:
#     print(li.text)

# lis = tree.xpath('//a[contains(text(),"京")]')
# for li in lis:
#     print(li.text)


# list = tree.xpath('//a[starts-with(@href,"http")]')
# for li in list:
#     print(li.text)

list = tree.xpath('//a[starts-with(text(),"京")]')
for li in list:
    print(li.text)
